//
//  HRFollowDynamicViewModel.m
//  Hear
//
//  Created by mac on 2017/5/19.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRFollowDynamicViewModel.h"
#import "HRDynamicModel.h"
#import "HRFollowDynamicLayout.h"
#import "HRDynamicCommentModel.h"
#import "Track.h"
#import "MTMusicFeature.h"
@implementation HRFollowDynamicViewModel

-(void)getFollowDynamicList:(NSDictionary*)requstDict andUrl:(NSString *)url resultArray:(void(^)(NSMutableArray *resultArr))resultArray{
    
    YTKApi *api = [YTKApi apiGetWithUrl:url para:requstDict];//
//    api.cacheTime = 1;

    [api getSuccessToken:^() {
        api.foceNoTocken = NO;
        [api setRequestSerializerType:YTKRequestSerializerTypeJSON];
        [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
            [[NSUserDefaults standardUserDefaults] setInteger:300 forKey:@"cacheTime"];

            NSDictionary *dict = request.responseJSONObject;
            NSArray *resultArr = [NSArray modelArrayWithClass:[HRDynamicModel class] json:dict[@"BodyContent"]];
            NSMutableArray *layouts = [NSMutableArray array];
            for (HRDynamicModel *model in resultArr) {
                
                
                if([model.MomentType intValue] == 2){
                    model.pics = [model.MomentDetai jsonValueDecoded];
                }else if ([model.MomentType intValue] == 5){
                    model.soundAndImageConentModel = [HRDynamicModel modelWithJSON:model.MomentDetai];
                    model.soundAndImageConentModel.pics = [model.soundAndImageConentModel.PhotoUrl jsonValueDecoded];
                    
                    Track *track = [[Track alloc] init];
                    track.audioFileURL = [NSURL URLWithString:model.soundAndImageConentModel.MomentDetai];
                    model.soundAndImageConentModel.musicFeature = [[MTMusicFeature alloc] initMusicFeatureWithAudioFile:track];
                    
                }else if ( [model.MomentType intValue] == 1){
                    Track *track = [[Track alloc] init];
                    track.audioFileURL = [NSURL URLWithString:model.MomentDetai];
                    model.musicFeature = [[MTMusicFeature alloc] initMusicFeatureWithAudioFile:track];
                }else if ([model.MomentType intValue] == 6){
                    model.MomentDetaiModel = [HRDynamicModel modelWithJSON:model.MomentDetai];
                }                
                
                model.comentListArray = [NSArray modelArrayWithClass:[HRDynamicCommentModel class] json:model.commentList];
                
                HRFollowDynamicLayout *layout = [HRFollowDynamicLayout new];
                layout.dynamicModel = model;
                [layouts addObject:layout];
                
            }
            
            

            resultArray(layouts);
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
            resultArray(@[]);
        }];
    }];
}

-(void)getDynamicAnswerList:(HRBaseRequstDataModel *)requstModel andUrl:(NSString *)url resultArray:(void(^)(NSMutableArray *resultArr))resultArray{
    YTKApi *api = [YTKApi apiGetWithUrl:url parameter:requstModel];//
    api.cacheTime = 1;
    [api getSuccessToken:^() {
        api.foceNoTocken = NO;
        [api setRequestSerializerType:YTKRequestSerializerTypeJSON];
        [api startWithCompletionBlockWithSuccess:^(__kindof YTKBaseRequest * _Nonnull request) {
            
            NSDictionary *dict = request.responseJSONObject;
            NSArray *resultArr = [NSArray modelArrayWithClass:[HRDynamicModel class] json:dict[@"BodyContent"]];
            NSMutableArray *layouts = [NSMutableArray array];
            for (HRDynamicModel *model in resultArr) {
                model.isFromHistoryAnswer = YES;
//                model.MomentNotes = model.QuestionText;
                if([model.MomentType intValue] == 2){
                    model.pics = [model.MomentDetai jsonValueDecoded];
                }else if ([model.MomentType intValue] == 5){
                    model.soundAndImageConentModel = [HRDynamicModel modelWithJSON:model.MomentDetai];
                    model.soundAndImageConentModel.pics = [model.soundAndImageConentModel.PhotoUrl jsonValueDecoded];
                    
                    Track *track = [[Track alloc] init];
                    track.audioFileURL = [NSURL URLWithString:model.soundAndImageConentModel.MomentDetai];
                    model.soundAndImageConentModel.musicFeature = [[MTMusicFeature alloc] initMusicFeatureWithAudioFile:track];
                    
                }else if ( [model.MomentType intValue] == 1){
                    Track *track = [[Track alloc] init];
                    track.audioFileURL = [NSURL URLWithString:model.MomentDetai];
                    model.musicFeature = [[MTMusicFeature alloc] initMusicFeatureWithAudioFile:track];
                }else if ([model.MomentType intValue] == 6){
                    model.MomentDetaiModel = [HRDynamicModel modelWithJSON:model.MomentDetai];
                }
                
                
                
                model.comentListArray = [NSArray modelArrayWithClass:[HRDynamicCommentModel class] json:model.commentList];
                
                HRFollowDynamicLayout *layout = [HRFollowDynamicLayout new];
                layout.dynamicModel = model;
                [layouts addObject:layout];
                
            }
            
            
            
            resultArray(layouts);
        } failure:^(__kindof YTKBaseRequest * _Nonnull request) {
            resultArray(@[]);
        }];
    }];
}
@end
